package goods;

import enums.GoodsStatus;
import enums.GoodsType;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.db.jpa.JPA;
import play.db.jpa.Model;
import product.Product;
import product.ProductSpec;
import product.type.AdminProductType;
import remind.TimeRemindEnclosure;

import javax.persistence.*;
import java.util.Date;


/**
 * 商品.
 */
@Entity
@Table(name = "goods")
public class Goods extends Model {

    /**
     * 商品名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 状态，包括：NEW/OPEN/CLOSE
     */
    @Enumerated(EnumType.ORDINAL)
    public GoodsStatus status;

    /**
     * Goods 类型。 目前只有一种 product
     */
    @Enumerated(EnumType.STRING)
    public GoodsType type;

    /**
     * productSpecMerchant Id
     */
    @Column(name = "serial_id")
    public Long serialId;

    /**
     * 产品型号
     */
    @Column(name = "spec")
    public String spec;

    /**
     * 单位
     */
    @Column(name = "unit")
    public String unit;

    /**
     * 商品编码/条形码
     */
    @Column(name = "code")
    public String code;

    /**
     * 最大购买数量
     */
    @Column(name = "max_num")
    public Integer maxNum;
    /**
     * 商品标题
     */
    @Column(name = "title")
    public String title;

    /**
     * 商品关键字
     */
    @Column(name = "keywords")
    public String keywords;

    /**
     * 商品简介
     */
    @Column(name = "summary")
    public String summary;

    /**
     * 商品详情
     */
    @Column(name = "details")
    public String details;

    /**
     * 主图片路径
     */
    @Column(name = "main_image_url")
    public String img;

    /**
     * 开始销售时间
     */
    @Column(name = "begin_onsale_at")
    public Date beginOnsaleAt;

    /**
     * 结束销售时间
     */
    @Column(name = "end_onsale_at")
    public Date endOnsaleAt;

    /**
     * 成本价
     */
    @Column(name = "original_price")
    public Double originalPrice;

    /**
     * 合伙人价格
     */
    @Column(name = "partner_price")
    public Double partnerPrice;

    /**
     * 销售单价
     */
    @Column(name = "sale_price")
    public Double salePrice;

    /**
     * 排列顺序
     */
    @Column(name = "display_order")
    public Integer displayOrder;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 订单创建时间
     */
    @Column(name = "created_at")
    public Date createdAt;

    /**
     * 更新时间
     */
    @Column(name = "updated_at")
    public Date updatedAt;

    /**
     * 更新对象.
     */
    public static Goods update(Long id, Goods newObject) {
        Goods goods = Goods.findById(id);
        if (goods == null) {
            return null;
        }
        goods.name = newObject.name;
        goods.save();
        return goods;
    }

    public static Boolean delete(Long id) {
        Goods goods = Goods.findById(id);
        if (goods == null) {
            return Boolean.FALSE;
        }
        goods.deleted = DeletedStatus.DELETED;
        goods.save();
        return Boolean.TRUE;
    }


    public static Goods getGoodsByProductId(Long productId){
        return Goods.find("deleted = ? and serial = ? " , DeletedStatus.UN_DELETED , "PRODUCT_"+productId).first();
    }
    /**
     * 把响应的goods 相关的产品ID 返回
     * @param goods
     * @return
     */
    public static Long separationId(Goods goods) {
        Long productId = goods.serialId;
        if(productId != null && productId > 0) {
            return productId;
        }
        return productId;
    }

    public static void updateCodeBySpec(ProductSpec spec) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update goods g set g.code = '"+ spec.barCode +"' where g.serial_id in (select psm.id from product_spec_merchants psm where psm.product_spec_id = " +spec.id+ ")");
        Logger.info("Update SQL : %s :" , stringBuilder.toString());
        JPA.em().createNativeQuery(stringBuilder.toString()).executeUpdate();
    }

    /**
     * 根据商品Id删除商品
     * @param specMerchantIds
     */
    public static void deleteBySpecMerchant(String specMerchantIds){
        String updateSql = " update  goods a set a.deleted = 1 where a.id in  ("+specMerchantIds+")" ;
        JPA.em().createNativeQuery(updateSql).executeUpdate();
    }


}
